package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaobaibai.entity.User;
import com.xiaobaibai.entity.UserAddress;
import com.xiaobaibai.entity.UserInfo;
import com.xiaobaibai.entity.UserIntegralDetail;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IUserAddressService;
import com.xiaobaibai.service.IUserInfoService;
import com.xiaobaibai.service.IUserIntegralDetailService;
import com.xiaobaibai.service.IUserService;
import com.xiaobaibai.threadPool.MyThreadPoolUnit;
import com.xiaobaibai.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/user")
@Slf4j
@CrossOrigin
public class WxUserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserAddressService userAddressService;

    @Autowired
    private IUserIntegralDetailService userIntegralDetailService;

    @GetMapping("/list")
    @ApiOperation("查询所有用户")
    public ResponseFormat getUserList(){

        QueryWrapper<User> q1=new QueryWrapper<>();
        q1.select("user_id","user_image","user_name","user_gender","user_last_login","user_create_time");
        q1.orderByDesc("user_id");//倒序
        List<User> userList = userService.list(q1);
        QueryWrapper<UserInfo> q2=new QueryWrapper<>();
        q2.select("user_id","user_amount","user_integral","user_order_quantity");
        List<UserInfo> userInfoList = userInfoService.list();
        Map<Integer,UserInfo> map=new HashMap<>(userInfoList.size());
        userInfoList.forEach(i->{
            map.put(i.getUserId(),i);
        });

        List<UserVo> userVoList=new ArrayList<>(userList.size());

        userList.forEach(u->{
            UserVo userVo = UserVo.coverUser(u, map.get(u.getUserId()));
            userVoList.add(userVo);
        });

        return ResponseFormat.ok("获取用户信息成功",userVoList);
    }

    /**
     * 用户详细里面要有什么?
     * 1.用户的近期订单
     * 2.用户的地址
     * 3.用户的积分详情
     * 4.用户的评价
     * 5.用户的优惠券情况
     * 6.用户的砍价情况
     */

    @GetMapping("/detail/{id}")
    @ApiOperation("查看用户详细")
    public ResponseFormat getUserDetail(@PathVariable(value = "id")Integer userId) throws ExecutionException, InterruptedException {
        Map<String,Object> map=new HashMap<>();

        //基本信息
        QueryWrapper<User> q0 = new QueryWrapper<>();
        q0.select("user_id", "user_name", "user_gender", "user_last_login", "user_create_time");
        q0.eq("user_id", userId);
        User user = userService.getOne(q0);
        map.put("user", user);

        CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
            //详细信息
            QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
            queryWrapper.select("user_amount","user_integral","user_order_quantity","user_last_buy");
            queryWrapper.eq("user_id",userId);
            UserInfo userInfo = userInfoService.getOne(queryWrapper);
            map.put("userInfo",userInfo);
        }, MyThreadPoolUnit.threadPoolExecutor);

        CompletableFuture<Void> c2 = CompletableFuture.runAsync(() -> {
            //地址信息
            QueryWrapper<UserAddress> q2=new QueryWrapper<>();
            q2.select("user_name","user_phone","user_address_detail","flag");
            q2.eq("user_id",userId);
            List<UserAddress> list = userAddressService.list(q2);
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getFlag()){
                    //交换位置
                    if(i==0)
                        break;
                    else{
                        UserAddress temp=list.get(0);
                        list.set(0,list.get(i));
                        list.set(i,temp);
                        break;
                    }
                }
            }
            map.put("address",list);
        }, MyThreadPoolUnit.threadPoolExecutor);

        CompletableFuture<Void> c3 = CompletableFuture.runAsync(() -> {
            //积分详细信息
            QueryWrapper<UserIntegralDetail> q3=new QueryWrapper<>();
            q3.select("integral_content","integral_flag","integral_quantity","create_time");
            q3.orderByDesc("id");
            q3.eq("user_id",userId);
            List<UserIntegralDetail> userIntegralDetailList = userIntegralDetailService.list(q3);
            map.put("userIntegral",userIntegralDetailList);
        }, MyThreadPoolUnit.threadPoolExecutor);

        CompletableFuture<Void> all = CompletableFuture.allOf(c1, c2, c3);
        all.get();//等待

        return ResponseFormat.ok("用户详细信息",map);
    }

}
